package print;

/**
 * @author gonghl
 * @date 2019/1/25
 */
public class PrintUtils {
    private static final String DEFAULT_EDGE = "+";
    private static final String DEFAULT_FILL = "-";
    private static final String DEFAULT_LEFT_AND_RIGHT = "|";
    private static final int DEFAULT_MARGIN = 20;
    private static final String DEFUALT_PERCENT_SEP = "|";
    private static int i = 0;
    private static Thread thread;
    private static boolean isStop = false;

    /**
     * 打印校验结果
     *
     * @param content 内容
     */
    public static void printTipInfo(String content) {
        printResult(content, DEFAULT_MARGIN);
    }

    /**
     * 显示进度条
     * @param percent 进度比
     */
    public static void showProcess(int percent) {
        if (percent > 100 || percent < 0) {
            return;
        }
        System.out.print("\r");
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < percent; i++) {
            if (i % 2 == 0) {
                buffer.append(DEFUALT_PERCENT_SEP);
            }
        }
        System.out.printf("%-52s", buffer.toString());
        System.out.print(percent + "% ");
    }

    /**
     * 显示等待图标
     */
    public static void showLoadding() {
        thread = new Thread(() -> {
            while (!isStop) {
                System.out.print("\rPlease Wait ...  ");

                if (i == 0) {
                    System.out.print("|");
                }

                if (i == 1) {
                    System.out.print("/");
                }

                if (i == 2) {
                    System.out.print("—");
                }

                if (i == 3) {
                    System.out.print("\\");
                }
                try {
                    Thread.sleep(300L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i = ++i % 4;
            }
        });
        thread.start();
    }

    public static void stopLoadding() {
        isStop = true;
        System.out.print("\r");
    }

    private static void printResult(String tip,int margin) {
        int width = tip.length() + margin * 2;
        printLine(width, DEFAULT_EDGE, DEFAULT_FILL);
        System.out.println();
        printLeftAndRight(width);
        System.out.println();

        printContent(tip,margin);
        System.out.println();

        printLeftAndRight(width);
        System.out.println();
        printLine(width, DEFAULT_EDGE, DEFAULT_FILL);
    }

    private static void printLine(int width, String edge,String fill) {
        System.out.print(edge);
        for (int i = 0; i < width; i++) {
            System.out.print(fill);
        }
        System.out.print(edge);
    }

    private static void printLeftAndRight(int width) {
        System.out.printf("%-1s",DEFAULT_LEFT_AND_RIGHT);
        System.out.printf("%" + (width + 1) + "s",DEFAULT_LEFT_AND_RIGHT);
    }

    private static void printContent(String content,int margin) {
        System.out.printf("%-1s",DEFAULT_LEFT_AND_RIGHT);
        System.out.printf("%" + (content.length() + margin) + "s", content);
        System.out.printf("%" + (margin + 1) + "s",DEFAULT_LEFT_AND_RIGHT);
    }
}
